
package com.joysuch.wwyt.core.repository;

import com.joysuch.wwyt.core.entity.BaseAttachmentRepo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * BaseImageRepository
 *
 */
public interface BaseAttachmentRepoDao extends JpaRepository<BaseAttachmentRepo, Long> {

    public Page<BaseAttachmentRepo> findAll(Specification<BaseAttachmentRepo> spec, Pageable pageable);

    public BaseAttachmentRepo save(BaseAttachmentRepo bean);

    public void delete(BaseAttachmentRepo bean);

   BaseAttachmentRepo findFirstByFingerPrint(String fingerPrint);

    List<BaseAttachmentRepo> findByFingerPrint(String fingerPrint);

    List<BaseAttachmentRepo> findByFileNameAndFingerPrint(String originalFilename, String fingerPrint);

    @Query(value = "select * from wwyt_base_attachment_repo where file_name = ?1 and finger_print = ?2 order by id desc limit 1", nativeQuery = true)
    BaseAttachmentRepo getOneByFileNameAndFingerPrint(String originalFilename, String fingerPrint);

    @Query("select b from BaseAttachmentRepo b where b.id in (?1)")
    List<BaseAttachmentRepo> findByIds(List<Long> ids);

    @Transactional
    @Modifying
    @Query(value = "update wwyt_base_attachment_repo set integrity=0 where id=:id", nativeQuery = true)
    public int updateIntegrityById(@Param("id") Long id);

    @Query(value = "SELECT *FROM wwyt_base_attachment_repo WHERE DELETE_FLAG='0' and CONVERT_STATE=3 and retry_count<?1 order by id desc limit 1", nativeQuery = true)
    BaseAttachmentRepo findBaseAttachmentRepo(Integer retryCount);
}
